package com.fenghuajueli.module_user.activity.delete;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants;
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.listener.BaseCallBackListener;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.databinding.ActivityDeleteAccountBinding;
import com.fenghuajueli.module_user.model.UserViewModel;
import com.fenghuajueli.module_user.network.result.TemplateResult;

import org.greenrobot.eventbus.EventBus;

import static com.fenghuajueli.module_user.activity.login.LoginActivity.KEY_APPLY_PRIVACY;

/**
 * 新版本为了兼容一键登录  不做密码验证了 只验证手机号
 */
@Route(path = UserModuleRoute.USER_DELETE_ACCOUNT)
public class DeleteAccountActivity extends UserViewModelBaseActivity implements BaseCallBackListener<String>, View.OnClickListener {

    ActivityDeleteAccountBinding binding;
    private final UserViewModel userViewModel = new UserViewModel();


    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDeleteAccountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (UserInfoUtils.getInstance().isLogin()) {
            String phone = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getPhone();
            if (!TextUtils.isEmpty(phone)) {
                binding.etInputPhone.setText(phone);
            }
        }

        binding.btnComplete.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        binding.btnSendCode.setOnClickListener(this);
        userViewModel.verificationCodeTime.observe(this, time -> {
            binding.btnSendCode.setText(time + "s");
            binding.btnSendCode.setEnabled(false);
            if (time == 0) {
                binding.btnSendCode.setEnabled(true);
                binding.btnSendCode.setText("获取验证码");
            }
        });
        userViewModel.templateResult.observe(this, result -> {
            if (result instanceof TemplateResult.Success){
                String themeColors = ((TemplateResult.Success) result).getData().getTheme_color().getTheme_colors();
                binding.btnComplete.getBackground().setTint(Color.parseColor(themeColors));
                binding.btnSendCode.getBackground().setTint(Color.parseColor(themeColors));
            }
        });
    }


    @Override
    public void onSuccess(String data) {
//        ToastUtils.showShort("账户注销成功！");
        UserInfoUtils.getInstance().clearLoginInfo();
        EventBus.getDefault().post(new EventEntity(EventBusConstants.DELETE_USER, null));
        hideLoadingDialog();

        finish();
    }

    @Override
    public void onFailed(String error) {
        ToastUtils.showShort(error);
        hideLoadingDialog();
    }

    @Override
    public void onHandlerStart() {
        showLoadingDialog("账户注销中...");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnComplete) {
            if (userViewModel != null) {
                userViewModel.deleteAccount(DeleteAccountActivity.this, UserInfoUtils.getInstance().getUserInfoEntity().getToken()
                        , UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getUser_id()
                        , binding.etInputPhone.getText().toString().trim(),
                        binding.etInputCode.getText().toString().trim());
                MmkvUtils.save(KEY_APPLY_PRIVACY, false);
            }
        } else if (view.getId() == R.id.ivClose) {
            finish();
        } else if (view.getId() == R.id.btnSendCode) {
            if (userViewModel != null) {
                userViewModel.getVerificationCode(this, binding.etInputPhone.getText().toString().trim());
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}